package com.china08.yxyapi.api.manage.tec.teaching.lecture.prepare;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack.Res;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.lecture.YxyLecturePrePackRepository;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 老师 ---备授课--查询推荐备课包
 * 
 */
@CrossOrigin
@RestController
public class LectureSchRePrePackController extends YxyApiBaseController {

    @Autowired
    private YxyLecturePrePackRepository yxyLecturePrePackRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/reprepack/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel req) {
        String schoolId = getProfile(request, response).getSchoolId();
        String bookContentsId = req.bookContent;
        Integer status = 0;
        Integer share = 1;
        Integer isYx = 0;
        Integer isYx1 = 1;

        YxyLecturePrePack prePack = yxyLecturePrePackRepository
                .findTop1ByIsYxAndBookcontentsIdAndStatusNotAndShareOrderByUsedCountDesc(isYx, bookContentsId, status,
                        share);
        if (null == prePack) {
            prePack = yxyLecturePrePackRepository
                    .findTop1ByIsYxAndBookcontentsIdAndStatusNotAndShareOrderByUsedCountDesc(isYx1, bookContentsId,
                            status, share);
        }
        RespModel model = new RespModel();
        if (prePack != null) {
            model.id = prePack.getId();
            model.title = prePack.getPackName();
            model.count = prePack.getResources() == null ? 0 : prePack.getResources().size();
            model.schoolNick = prePack.getSchoolNick();
            if (schoolId.equals("ff80808142a16af00142a1a60f010006")
                    || "ff80808142a16af00142a1a60f010006".equals(prePack.getSchoolId())) {
                model.schoolNick = "";
            }
            model.userNick = prePack.getUserNick();
            model.share = prePack.getShare();
            model.usedCount = prePack.getUsedCount();
            model.pv = prePack.getPv();
            model.createdDateFormat = DateToolUtils.format(prePack.getLastModifiedDate(),
                    DateToolUtils.FORMAT_SHORT_YMD);
            List<ResModel> resModels = new ArrayList<ResModel>();
            if (prePack.getResources() != null && prePack.getResources().size() > 0) {
                for (Res respModel : prePack.getResources()) {
                    ResModel resModel = new ResModel();
                    resModel.formatId = respModel.getFormatId();
                    resModel.resourceName = respModel.getResourceName();
                    resModel.courseType = respModel.getCourseType();
                    resModels.add(resModel);
                }
            }
            model.resources = resModels;
        }

        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {

        public String bookContent;

    }

    protected class ResModel {

        public String formatId;

        public String resourceName;

        public Integer courseType;

    }

    protected static class RespModel extends YxyApiRespModel {

        public String id;

        public String title;

        public int count;
        // 贡献者学校名称
        public String schoolNick;
        // 贡献者名称
        public String userNick;

        public Long usedCount;

        public Long pv;
        // 创建时间（收录时间）
        public String createdDateFormat;

        public Integer share;
        // 资源
        public List<ResModel> resources;

    }

}
